package com.bdsy.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bdsy.business.dao.UserIntegralsMapper;
import com.bdsy.business.model.UserIntegrals;
import com.bdsy.business.service.UserIntegralsService;

@Service("userIntegralsService")
public class UserIntegralsServiceImpl implements UserIntegralsService {

	@Autowired
	UserIntegralsMapper UserIntegralsMapper;
	
	@Override
	public List<UserIntegrals> selectUserIntegralsByUserId(String userId) {
		List<UserIntegrals> UserIntegrals = UserIntegralsMapper.selectUserIntegralsByUserId(userId);
		return UserIntegrals;
	}

	@Override
	public boolean update(UserIntegrals userIntegrals) {
		/**
		 * 根据用户编号查询当前用户累计消费金额、积分 使积分与金额一致  金额积分兑换比例 100:1
		 * 根据所获的的金额按照比例兑换成积分 完成金额、积分修改 
		 * 如果用户的积分 <1000  0  1000=< <=5000  1  5000=< <=10000  2  >10000  3  用户等级获得提升
		 */
		List<UserIntegrals> userInte = UserIntegralsMapper.selectUserIntegralsByUserId(userIntegrals.getUserId());
		String transAmount = "";
		String integrals = "";
		String levelImg = "";
		//查出来的 金额 积分
		for (UserIntegrals userInt : userInte) {
			transAmount = userInt.getTransAmount();
			integrals = userInt.getIntegrals();
			levelImg = userInt.getLevelImg();
		}
		
		//计算积分
		BigDecimal num1 = new BigDecimal(userIntegrals.getTransAmount());// 新金额
		BigDecimal num2 = new BigDecimal(100);// 比例
		BigDecimal integrals2 = num1.divide(num2);//积分
		
		BigDecimal trans = new BigDecimal(transAmount);
		BigDecimal inte = new BigDecimal(integrals);
		//更新 累计金额
		userIntegrals.setTransAmount(trans.add(num1).toString());
		//更新 累计积分
		String ninte = inte.add(integrals2).toString(); //新积分
		userIntegrals.setIntegrals(ninte);
		
		//截取字符串头部
		String levelImgs =	StringUtils.substringBeforeLast(levelImg, ".") ;
		String head_levelIm = levelImgs.substring(0, levelImgs.length()-1);
			//截取字符串尾部
		String end_levelImg = StringUtils.substringAfterLast(levelImg, ".");
		System.out.println(levelImgs);
		System.out.println(head_levelIm);
	    System.out.println(end_levelImg);
		
	    String nlevelImg = "";
		//如果积分大于指定值 会员等级提升
		if(Integer.parseInt(ninte) < 1000){
			userIntegrals.setLevel(0);
			//解析图片字符串 做拼接
//		    nlevelImg = head_levelIm+"1"+"."+end_levelImg;
		}else if(Integer.parseInt(ninte) >=1000 && Integer.parseInt(ninte) <5000){
			userIntegrals.setLevel(1);
			nlevelImg = head_levelIm+"1"+"."+end_levelImg;
		}else if(Integer.parseInt(ninte) >=5000 && Integer.parseInt(ninte) <10000){
			userIntegrals.setLevel(2);
			nlevelImg = head_levelIm+"2"+"."+end_levelImg;
		}else if(Integer.parseInt(ninte) >=10000){
			userIntegrals.setLevel(3);
			nlevelImg = head_levelIm+"3"+"."+end_levelImg;
		}
		
		userIntegrals.setLevelImg(nlevelImg);
		
		int result = UserIntegralsMapper.update(userIntegrals);
		return result>0?true:false;
	}

	@Override
	public boolean insert(UserIntegrals userIntegrals) {
		userIntegrals.setId((int)((Math.random()*9+1)*10000000));
		userIntegrals.setLevel(0);
		userIntegrals.setIntegrals(Integer.toString(0));
		userIntegrals.setLevelImg("/level/icon_VIP1.jpg");
		int result = UserIntegralsMapper.insert(userIntegrals);
		return result>0?true:false;
	}

}
